﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FBProject.BusinessLogic
{
    public class ApplicationHelper
    {
        internal static string GetApplicationNameAndVersion(System.Reflection.AssemblyName assemblyName)
        {
            var applicationNameAndVersionBuilder = new StringBuilder();
            applicationNameAndVersionBuilder.Append(assemblyName.Name);

            applicationNameAndVersionBuilder.AppendFormat(" v{0}.{1}", assemblyName.Version.Major, assemblyName.Version.Minor);

            if (assemblyName.Version.Build < 3)
            {
                applicationNameAndVersionBuilder.Append(" Alfa");
            }
            else if (assemblyName.Version.Build < 5)
            {
                applicationNameAndVersionBuilder.Append(" Beta");
            }

            return applicationNameAndVersionBuilder.ToString();
        }

        internal static string GetFormattedDateTimeDifference(DateTime dateTimeUTC)
        {
            var formattedDateTime = new StringBuilder();

            var timeDifference = DateTime.UtcNow.Subtract(dateTimeUTC);

            if (timeDifference.TotalDays >= 1)
            {
                formattedDateTime.AppendFormat("{0:N0} day{1} ago", timeDifference.TotalDays, timeDifference.TotalDays > 1 ? "s" : string.Empty);
            }
            else if (timeDifference.TotalHours >= 1)
            {
                formattedDateTime.AppendFormat("{0:N0} hour{1} ago", timeDifference.TotalHours, timeDifference.TotalHours > 1 ? "s" : string.Empty);
            }
            else if (timeDifference.TotalMinutes >= 1)
            {
                formattedDateTime.AppendFormat("{0:N0} minute{1} ago", timeDifference.TotalMinutes, timeDifference.TotalMinutes > 1 ? "s" : string.Empty);
            }
            else
            {
                formattedDateTime.Append("< 1 minute ago");
            }

            return formattedDateTime.ToString();
        }


        internal static string GetFormattedFileSize(long fileSize)
        {
            var recalculatedFileSize = (double)fileSize;
            var unit = "B";

            if (recalculatedFileSize > 1024)
            {
                recalculatedFileSize /= 1024;
                unit = "KB";
                if (recalculatedFileSize > 1024)
                {
                    recalculatedFileSize /= 1024;
                    unit = "MB";
                    if (recalculatedFileSize > 1024)
                    {
                        recalculatedFileSize /= 1024;
                        unit = "GB";
                    }
                }
            }

            return string.Format("{0:N1} {1}", recalculatedFileSize, unit);
        }
    }
}
